package com.chigga.application.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * Favicon 拦截器
 */
@Component
@Slf4j
public class FaviconInterceptor implements HandlerInterceptor {
    /**
     * 预处理
     * @param request 请求
     * @param response 响应
     * @param handler 处理器
     */
    @Override
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) throws Exception {
        // 处理 no static resource favicon.ico 控制台警告
        if("GET".equalsIgnoreCase(request.getMethod()) && "/favicon.ico".equals(request.getRequestURI())) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            return false;
        }
        // return true 放行
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * 后处理
     * @param request 请求
     * @param response 响应
     * @param handler 处理器
     */
    @Override
    public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 视图渲染完成
     * @param request 请求
     * @param response 响应
     * @param handler 处理器
     */
    @Override
    public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
